package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * 折扣费用属性处理器
 */
public class ProcessingOrderChargesAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
        if(KiteObjectUtils.isEmpty(prodInst)){
            return;
        }
        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
        String fieldName = attr.getFieldName();
        if(KiteListUtils.isNotEmpty(funcs) && (KiteStringUtils.equals("200003717",fieldName)||KiteStringUtils.equals("200004185",fieldName))){
            String communicationPacketType = "";
            for(KiteProdInstFunc func : funcs){
                //通信套包类型
                KiteAttr kiteAttr = func.getAttr("200003697");
                if (Objects.isNull(kiteAttr)) {
                    // 资费套包类型
                    kiteAttr = func.getAttr("200004179");
                }
                if(!KiteObjectUtils.isEmpty(kiteAttr)){
                    communicationPacketType = kiteAttr.getValue();
                }
            }
            //折扣类型
            String discountType = "";
            KiteAttr kiteAttr = inst.getAttr("200003712");
            if(!KiteObjectUtils.isEmpty(kiteAttr)){
                discountType = kiteAttr.getValue();
            }
            //通信套包类型 月付固定共享、预付固定共享
            if(KiteStringUtils.equals("200003717",fieldName)){
                if(!(KiteStringUtils.equals("100007469",communicationPacketType)||KiteStringUtils.equals("100007470",communicationPacketType))){
                    //折扣类型为一口价时展示0-∞万户数量档最终账户费(元)
                    if(KiteStringUtils.equals("100006437",discountType)){
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }else{
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                }else {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                }
            }else{
                if(KiteStringUtils.equals("100007469",communicationPacketType)||KiteStringUtils.equals("100007470",communicationPacketType)){
                    //折扣类型为一口价时展示0-∞万户数量档最终账户费(元)
                    if(KiteStringUtils.equals("100006437",discountType)){
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }else{
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                }else {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                }
            }
        }
    }
}
